//
//  SearchBarViewController.m
//  UI_HLJ_Frame
//
//  Created by  毕宏伟 on 15/7/8.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "SearchBarViewController.h"
#import "searchBarCollectionViewCell.h"
#import "AFNetworking.h"
#import "keywordsModel.h"
#import "searchDetailViewController.h"
#import "textFieldViewController.h"
@interface SearchBarViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITextFieldDelegate>

@property(nonatomic,retain)UIView *backView;
@property(nonatomic,retain)UILabel *remenLabel;
@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)NSMutableArray *keyArr;

@property(nonatomic,retain)UITextField *textfield;
@end

@implementation SearchBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    
    
    [self createData];
    [self createView];
   
    
    
}

- (void)createData
{
    self.keyArr =[NSMutableArray array];
    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
    [manager GET:@"http://hunliji.com/v1/api/app/keywords/hot.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic =responseObject;
        NSArray *keywordsArr = dic[@"keywords"];
        for (NSDictionary *keywordsdic in keywordsArr) {
            
            keywordsModel *key =[[keywordsModel alloc]init];
            [key setValuesForKeysWithDictionary:keywordsdic];
            [self.keyArr addObject:key];
            [key release];
            
            [self.collectionView reloadData];
//            NSLog(@"数据解析成功");
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    textFieldViewController *textVC =[[textFieldViewController alloc]init];
    textVC.nameUrl =self.textfield.text;
    [self.navigationController pushViewController:textVC animated:YES];
    [textVC release];
    
    return YES;
}

- (void)createView
{
    
    //创建textfield
    
    self.textfield= [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width/5, 20, self.view.frame.size.width-50, 30)];
    self.textfield.layer.cornerRadius =15;
    self.textfield.layer.borderWidth =1;
    self.textfield.placeholder =@"请输入关键字";
    self.textfield.textAlignment =1;
    self.textfield.delegate =self;
    self.textfield.clearButtonMode =UITextFieldViewModeAlways;
    self.textfield.returnKeyType =UIReturnKeySearch;
    self.navigationItem.titleView =self.textfield;
    [_textfield release];
    
    
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    self.backView.backgroundColor = [UIColor colorWithRed:0.943 green:0.300 blue:0.794 alpha:1.000];
    self.backView.alpha =0.5;
    [self.view addSubview:self.backView];
    [_backView release];
    
    self.remenLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 70, 30)];
    //    self.remenLabel.backgroundColor =[UIColor cyanColor];
    self.remenLabel.text =@"热门词";
    self.remenLabel.textAlignment =1;
    [self.view addSubview:self.remenLabel];
    [_remenLabel release];
    
    
    
    //创建一个collectionView
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize =CGSizeMake(self.view.frame.size.width/3,50);
    flow.minimumInteritemSpacing =0;
    flow.minimumLineSpacing =0;
    
    self.collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-49) collectionViewLayout:flow];
    self.collectionView.backgroundColor =[UIColor whiteColor];
    self.collectionView.delegate =self;
    self.collectionView.dataSource =self;
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    [self.collectionView registerClass:[searchBarCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}




#pragma mark 实现collectionView的点击方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   return self.keyArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    searchBarCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];

    
    keywordsModel *key =self.keyArr[indexPath.row];
    cell.searchLabel.text =key.name;
    
    return cell;
}



//实现collectionview点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    searchDetailViewController *serVC = [[searchDetailViewController alloc]init];
    keywordsModel *key =self.keyArr[indexPath.row];
    
    serVC.nameurl =key.name;
    [self.navigationController pushViewController:serVC animated:YES];
    [serVC release];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
